package com.chris.cc.gateway.controller;

import com.chris.cc.gateway.model.dto.RouteDefinitionDTO;
import com.chris.cc.gateway.service.wrapper.RouteServiceWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.web.bind.annotation.*;

/**
 * Copyright (c) 2018-2025, chris All rights reserved.
 * <p>
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 * <p>
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the pig4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * <p>
 * Author: haoka (haokang207@126.com)
 * <p>
 * Date: 2018/10/10 16:05
 * 路由管理
 **/
@RestController
@RequestMapping("/route")
@Slf4j
public class RouteController {
    @Autowired
    private RouteServiceWrapper routeServiceWrapper;

    /**
     * 查询
     *
     * @param page
     * @param limit
     * @return Page
     */
    @GetMapping
    public Object query(int page, int limit) {
        PageRequest pageRequest = PageRequest.of(page - 1, limit);
        return routeServiceWrapper.query(pageRequest);
    }


    /**
     * 根据id获取
     *
     * @param id
     * @return R
     */
    @GetMapping("/{id}")
    public Object get(@PathVariable("id") String id) {
        return routeServiceWrapper.get(id);
    }

    /**
     * 添加
     *
     * @param route
     * @return R
     */
    @PostMapping
    public void add(@RequestBody RouteDefinitionDTO route) {
        try {
            routeServiceWrapper.add(route);
        } catch (Exception e) {
            log.error("add route failed.", e);
        }
    }

    /**
     * 更新
     *
     * @param route
     * @return R
     */
    @PutMapping
    public void update(@RequestBody RouteDefinitionDTO route) {
        try {
            routeServiceWrapper.update(route);
        } catch (Exception e) {
            log.error("update route failed.", e);
        }
    }

    /**
     * 删除
     *
     * @param id
     * @return R
     */
    @DeleteMapping("/{id}")
    public void delete(@PathVariable String id) {
        try {
            routeServiceWrapper.delete(id);
        } catch (Exception e) {
            log.error("delete route failed", e);
        }
    }
}